package me.acomma.groot.domain.user;

import me.acomma.groot.infrastructure.dao.UserDao;
import me.acomma.groot.infrastructure.id.ID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserRepository {
    @Autowired
    private UserDao userDao;

    public User get(String username) {
        return userDao.selectByUsername(username);
    }

    public User get(UserId userId) {
        return userDao.select(userId);
    }

    public UserId nextId() {
        return new UserId(ID.next());
    }

    public void add(User user) {
        userDao.insert(user);
    }

    public void update(User user) {
        userDao.update(user);
    }

    public void remove(UserId userId) {
        userDao.delete(userId);
    }

    public List<User> pageQuery(User user) {
        return userDao.pagedQuery(user);
    }

    public void removeUserRoleByUserId(UserId userId) {
        userDao.deleteUserRoleByUserId(userId);
    }

    public void addUserRole(Long userId, List<Long> roleIds) {
        userDao.insertUserRole(userId, roleIds);
    }
}
